/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.GrupoactDAO;
import org.trandio.dao.GrupoactInterface;
import org.trandio.model.GrupoactVO;

/**
 *
 * @author root
 */
public class GrupoActividadListaServlet extends HttpServlet {

    private GrupoactInterface iface;
    private List<GrupoactVO> lista;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        iface = new GrupoactDAO();

        int cant = 0;
        int sEcho = Integer.parseInt(request.getParameter("sEcho"));
        int displayStart = Integer.parseInt(request.getParameter("iDisplayStart"));
        int displayLength = Integer.parseInt(request.getParameter("iDisplayLength"));
        String sSearch = request.getParameter("sSearch");

        String $sWhere = "";
        String[] $aColumns0 = {"id", "codigo", "descripcion"};
        String $sOrder = "";
        String orden = "";

        if (!request.getParameter("iSortCol_0").equals("")) {
            $sOrder = "ORDER BY  ";


            for (int i = 0; i < 3; i++) {
                if (request.getParameter("bSortable_" + i).equals("true")) {
                    $sOrder += $aColumns0[i] + " " + request.getParameter("sSortDir_0") + ", ";

                }
            }

            orden = $sOrder.substring(0, $sOrder.length() - 2);


            if (orden.equals("ORDER BY")) {
                orden = "";
            }
        }



        String[] $aColumns1 = {"id", "codigo", "descripcion"};

        for (int i = 0; i < 3; i++) {
            if (request.getParameter("bSearchable_" + i).equals("true") && !request.getParameter("sSearch_" + i).equals("")) {
                if ($sWhere.equals("")) {
                    $sWhere = "WHERE ";
                } else {
                    $sWhere += " AND ";
                }
                $sWhere += $aColumns1[i] + " LIKE '%" + request.getParameter("sSearch_" + i) + "%'";
            }
        }
        try {
            cant = iface.getCantGrupoActividades($sWhere);
            lista = iface.getGrupoActividades(displayStart, displayLength, $sWhere, orden);
        } catch (SQLException ex) {
            Logger.getLogger(GrupoActividadListaServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(GrupoActividadListaServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        Iterator pyp = lista.iterator();

        String resp = "";
        resp = "[";
        while (pyp.hasNext()) {
            GrupoactVO b = (GrupoactVO) pyp.next();
            resp += "[\"" + b.getId() + "\",\"" + b.getCodigo() + "\",\"" + b.getDescripcion() + "\"],";
        }
        resp = resp.substring(0, resp.length() - 1);
        resp += "]";
        String json = "{\"sEcho\":" + sEcho + ",\"iTotalRecords\":" + cant + ",\"iTotalDisplayRecords\":" + cant + ",\"aaData\":" + resp + "}";
        System.out.println(resp); 
        out.println(json);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
